package com.sanpu.kxe.business.main.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sanpu.library.imagechoose.PhotoPicker;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.bean.CertificationOutput;
import com.sanpu.kxe.business.main.contract.LinkfaceOcrContract;
import com.sanpu.kxe.business.main.presenter.LinkfaceOcrPresenter;
import com.sanpu.kxe.business.main.provider.MainOpenProvider;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.DialogViewListener;
import com.sanpu.kxe.jump.eventbus.ReviewPhotoEvent;
import com.sanpu.kxe.jump.imageloader.JKImageLoader;
import com.sanpu.kxe.module.roundview.RoundTextView;
import com.sanpu.kxe.utils.DialogUtil;
import com.sanpu.kxe.utils.FileUtil;
import com.sanpu.kxe.utils.ToastUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：身份证校验
 * Created by 薛勇军 on 2019/5/21.
 */

public class LinkfaceOcrActivity extends BaseTitleActivity implements LinkfaceOcrContract.View {

    @Bind(R.id.front)
    ImageView vFront;
    @Bind(R.id.front_image)
    ImageView vFrontImage;
    @Bind(R.id.front_image_cloud)
    TextView vFrontImageCloud;
    @Bind(R.id.reverse)
    ImageView vReverse;
    @Bind(R.id.reverse_image)
    ImageView vReverseImage;
    @Bind(R.id.reverse_image_cloud)
    TextView vReverseImageCloud;
    @Bind(R.id.commit)
    RoundTextView vCommit;
    @Bind(R.id.lastStepRtv)
    RoundTextView lastStepRtv;
    @Bind(R.id.idCardClaimTv)
    TextView idCardClaimTv;

    private LinkfaceOcrContract.Presenter mPresenter;

    private CertificationOutput mOutput;

    private boolean isNeedUpload = true;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_linkface_ocr;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("实名认证");
        mPresenter = new LinkfaceOcrPresenter(this);
    }

    @OnClick({R.id.commit, R.id.lastStepRtv, R.id.front, R.id.reverse})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.commit:
                if (vFrontImage.getTag() == null || vReverseImage.getTag() == null) {
                    ToastUtil.showTextViewPrompt("必须添加身份证正反面照片");
                    return;
                }
                if (!isNeedUpload && mOutput != null) {
                    showIdCardInfo(mOutput, isNeedUpload);
                } else {
                    mPresenter.uploadIdCardInfo();
                }
                break;
            case R.id.lastStepRtv:
                finish();
                break;
            case R.id.front:
                if (vFrontImage.getTag() != null) {
                    mPresenter.takePhoto((String) vFrontImage.getTag(),
                            CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_FRONT);
                } else {
                    MainOpenProvider.getInstance().openCertificateHelpActivity(
                            getContext(), CommonConfig.TYPE_IDFROPIC);
                }
                break;
            case R.id.reverse:
                if (vReverseImage.getTag() != null) {
                    mPresenter.takePhoto((String) vReverseImage.getTag(),
                            CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_REVERSE);
                } else {
                    MainOpenProvider.getInstance().openCertificateHelpActivity(
                            getContext(), CommonConfig.TYPE_IDCONPIC);
                }
                break;
        }
    }

    @Override
    public void showIdCardInfo(CertificationOutput output, boolean isNeedUpload) {
        this.mOutput = output;
        this.isNeedUpload = isNeedUpload;
        if (mOutput != null) {
            String message = "姓名：" + output.getIdCardName() + "\n身份证号：" + output.getIdCardNo();
            DialogUtil.getInstance().dialogExistTitleTwoBtnHaveCallBack(getContext(),
                    "信息核对", message, "取消", "确认", new DialogViewListener() {
                        @Override
                        public void btnRightConfirm() {
                            MainOpenProvider.getInstance().openLinkfaceCheckActivity(mContext, mOutput);
                        }

                        @Override
                        public void btnLeftCancel() {

                        }
                    });
        }
    }

    @Override
    public void onActivityResult(final int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && data != null) {
            ArrayList<String> photos =
                    data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
            switch (requestCode) {
                case CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_FRONT:
                    if (photos == null || photos.size() == 0) {
                        vFrontImage.setTag(null);
                        vFrontImage.setImageDrawable(null);
                        vFrontImage.setVisibility(View.GONE);
                        vFrontImageCloud.setVisibility(View.GONE);
                        mPresenter.removeFile(CommonConfig.TYPE_IDFROPIC);
                        isNeedUpload = true;
                    }
                    break;
                case CommonConfig.REQUESTCODE_CERTIFICATE_VIEW_REVERSE:
                    if (photos == null || photos.size() == 0) {
                        vReverseImage.setTag(null);
                        vReverseImage.setImageDrawable(null);
                        vReverseImage.setVisibility(View.GONE);
                        vReverseImageCloud.setVisibility(View.GONE);
                        mPresenter.removeFile(CommonConfig.TYPE_IDCONPIC);
                        isNeedUpload = true;
                    }
                    break;
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(ReviewPhotoEvent event) {
        if (event == null) return;
        String filePath = event.getFilePath();
        if (TextUtils.isEmpty(filePath)) {
            return;
        }
        isNeedUpload = true;
        String fileIdName;
        switch (event.getCurrentType()) {
            case CommonConfig.TYPE_IDFROPIC:
                fileIdName = CommonConfig.TYPE_IDFROPIC;
                JKImageLoader.getInstance().displayImage(getContext(), "file://" + filePath, vFrontImage);
                vFrontImage.setTag(filePath);
                vFrontImage.setVisibility(View.VISIBLE);
                vFrontImageCloud.setVisibility(View.VISIBLE);
                break;
            case CommonConfig.TYPE_IDCONPIC:
                fileIdName = CommonConfig.TYPE_IDCONPIC;
                JKImageLoader.getInstance().displayImage(getContext(), "file://" + filePath, vReverseImage);
                vReverseImage.setTag(filePath);
                vReverseImage.setVisibility(View.VISIBLE);
                vReverseImageCloud.setVisibility(View.VISIBLE);
                break;
            default:
                return;
        }
        mPresenter.onCompress(filePath, fileIdName);
    }

    @Override
    protected boolean hasBindEventBus() {
        return true;
    }

    @Override
    public void setPresenter(LinkfaceOcrContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FileUtil.deleteCache();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
